home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / utility / uwserver.zip / uwserver.tar / lib / uw_netadj.c < prev    next >
C/C++ Source or Header  |  1991-01-25  |  4KB  |  170 lines

  1. /*
  2.  *    uw library - uw_netadj
  3.  *
  4.  * Copyright 1986 by John D. Bruner.  All rights reserved.  Permission to
  5.  * copy this program is given provided that the copy is not sold and that
  6.  * this copyright notice is included.
  7.  */
  8. #include <sys/types.h>
  9. #include <sys/socket.h>
  10. #include <sys/un.h>
  11. #include <sys/ioctl.h>
  12. #include <sys/wait.h>
  13. #include <sys/time.h>
  14. #include <sys/resource.h>
  15. #include <sys/uio.h>
  16. #include <sys/file.h>
  17. #include <netinet/in.h>
  18. #include <strings.h>
  19. #include <signal.h>
  20. #include "openpty.h"
  21.  
  22. #include "uw_opt.h"    /* I had hoped to avoid including this */
  23. #include "uwlib.h"
  24.  
  25. static woptarg_t woa_vis[] = { WOA_UDATA(1), WOA_END };
  26. static woptarg_t woa_type[] = { WOA_UDATA(6), WOA_END };
  27. static woptarg_t woa_pos[] = { WOA_UDATA(12), WOA_UDATA(12), WOA_END };
  28. static woptarg_t woa_title[] = { WOA_STRING(255), WOA_END };
  29. static woptarg_t woa_size[] = { WOA_UDATA(12), WOA_UDATA(12), WOA_END };
  30. static woptarg_t woa_tsize[] = { WOA_UDATA(12), WOA_UDATA(12), WOA_END };
  31. static woptarg_t woa_fontsz[] = { WOA_UDATA(6), WOA_END };
  32. static woptarg_t woa_clipb[] = { WOA_UDATA(1), WOA_END };
  33. static woptarg_t woa_bell[] = { WOA_UDATA(2), WOA_END };
  34. static woptarg_t woa_curs[] = { WOA_UDATA(1), WOA_END };
  35. static woptarg_t woa_chgsz[] = { WOA_UDATA(1), WOA_END };
  36.  
  37. static woptarg_t *optargs[][WONUM_MAX+1] = {
  38.     /* window type 0 == adm31 */
  39.     {
  40.         0, woa_vis, woa_type, woa_pos, woa_title, woa_size, 0, 0,
  41.         woa_tsize, woa_fontsz, woa_clipb, woa_bell, woa_curs, woa_chgsz
  42.     },
  43.     /* window type 1 == vt52 */
  44.     {
  45.         0, woa_vis, woa_type, woa_pos, woa_title, woa_size, 0, 0,
  46.         woa_tsize, woa_fontsz, woa_clipb, woa_bell, woa_curs, woa_chgsz
  47.     },
  48.     /* window type 2 == ansi */
  49.     {
  50.         0, woa_vis, woa_type, woa_pos, woa_title, woa_size, 0, 0,
  51.         woa_tsize, woa_fontsz, woa_clipb, woa_bell, woa_curs, woa_chgsz
  52.     },
  53.     /* window type 3 = tek4010 */
  54.     {
  55.         0, woa_vis, woa_type, woa_pos, woa_title, woa_size, 0, 0,
  56.     },
  57.     /* window type 4 = file transfer */
  58.     {
  59.         0, woa_vis, woa_type, woa_pos, woa_title, woa_size, 0, 0,
  60.     },
  61.     /* window type 5 = printer */
  62.     {
  63.         0, woa_vis, woa_type, woa_pos, woa_title, woa_size, 0, 0,
  64.     },
  65.     /* window type 6 = plot */
  66.     {
  67.         0, woa_vis, woa_type, woa_pos, woa_title, woa_size, 0, 0,
  68.     },
  69. };
  70.  
  71. #ifdef htons
  72. uw_hton(wtype, optnum, data)
  73. uwtype_t wtype;
  74. uwopt_t optnum;
  75. char *data;
  76. {
  77. }
  78.  
  79. uw_ntoh(wtype, optnum, data)
  80. uwtype_t wtype;
  81. uwopt_t optnum;
  82. char *data;
  83. {
  84. }
  85.  
  86. #else
  87. /* These should have been defined in <netinet/in.h> but weren't (in 4.2BSD) */
  88. extern unsigned short htons(), ntohs();
  89. extern unsigned long htonl(), ntohl();
  90.  
  91. uw_hton(wtype, optnum, data)
  92. uwtype_t wtype;
  93. uwopt_t optnum;
  94. char *data;
  95. {
  96.     static struct netadj na = {
  97.         (short (*)())htons, (long (*)())htonl, htons, htonl
  98.     };
  99.     if (data != (char *)0 && wtype < sizeof optargs / sizeof optargs[0] &&
  100.         optnum <= WONUM_MAX && optargs[wtype][optnum] != (woptarg_t *)0) {
  101.         netadj(optargs[wtype][optnum], data, &na);
  102.     }
  103. }
  104.  
  105. uw_ntoh(wtype, optnum, data)
  106. uwtype_t wtype;
  107. uwopt_t optnum;
  108. char *data;
  109. {
  110.     static struct netadj na = {
  111.         (short (*)())ntohs, (long (*)())ntohl, ntohs, ntohl
  112.     };
  113.     if (data != (char *)0 && wtype < sizeof optargs / sizeof optargs[0] &&
  114.         optnum <= WONUM_MAX && optargs[wtype][optnum] != (woptarg_t *)0) {
  115.         netadj(optargs[wtype][optnum], data, &na);
  116.     }
  117. }
  118.  
  119. static
  120. netadj(woa, data, na)
  121. register woptarg_t *woa;
  122. char *data;
  123. register struct netadj *na;
  124. {
  125.     register char *cp;
  126.     register int cnt;
  127.     union {
  128.         struct {
  129.             char    c1;
  130.             short    s;
  131.         }    cs;
  132.         struct {
  133.             char    c2;
  134.             long    l;
  135.         }    cl;
  136.     } u;
  137.  
  138.     /*
  139.      * Convert an option between host byte order and network byte order.
  140.      */
  141.     if (data && na) {
  142.         for (cp=data; *woa != WOA_END; woa++) {
  143.             cnt = *woa & ~WOA_CMDMASK;
  144.             switch (*woa & WOA_CMDMASK) {
  145.             case WOA_CHARS(0):
  146.             case WOA_STRING(0):
  147.                 cp += cnt;
  148.                 break;
  149.             case WOA_UDATA(0):
  150.                 if (cnt <= NBBY) {
  151.                     cp++;
  152.                 } else if (cnt <= sizeof(short)*NBBY) {
  153.                     while ((int)cp & ((char *)&u.cs.s-&u.cs.c1-1))
  154.                         cp++;
  155.                     *(u_short *)cp =
  156.                         (*na->na_ushort)(*(u_short *)cp);
  157.                     cp += sizeof(short);
  158.                 } else {
  159.                     while ((int)cp & ((char *)&u.cl.l-&u.cl.c2-1))
  160.                         cp++;
  161.                     *(u_short *)cp =
  162.                         (*na->na_ushort)(*(u_short *)cp);
  163.                     cp += sizeof(long);
  164.                 }
  165.             }
  166.         }
  167.     }
  168. }
  169. #endif
  170.